#include <string.h>
#include <fcgi_stdio.h>
#include <stdlib.h>

#include "base.h"
#include "fcgiapp.h"
#include "http_echo.h"
#include "error_code.h"

char *
dp_strerror(int errcode, char *buf, int buflen)
{
	switch (errcode) {
		case ERROR_CODE_ALREADY_EXISTS:
			strncpy(buf, "resourse already exists", buflen);
			break;
		case ERROR_CODE_ILLEGAL_ARGUMENT:
			strncpy(buf, "illeal argumen", buflen);
			break;
		case ERROR_CODE_IOERROR:
			strncpy(buf, "io error", buflen);
			break;
		case ERROR_CODE_JSON_INVALID:
			strncpy(buf, "json string invalid", buflen);
			break;
		case ERROR_CODE_SOCKET_CLOSED:
			strncpy(buf, "server unavailable", buflen);
			break;
		case ERROR_CODE_SERVER_BUSY:
			strncpy(buf, "server busy", buflen);
			break;
		case ERROR_CODE_EMPTY:
			strncpy(buf, "resource empty", buflen);
			break;
		case ERROR_CODE_NOT_OPENED:
			strncpy(buf, "connect not open", buflen);
			break;
		case ERROR_CODE_INVALID_SCHEMA:
			strncpy(buf, "database schema invalid", buflen);
			break;
		case ERROR_CODE_DATA_STATUS_UNKNOWN:
			strncpy(buf, "data status unknown", buflen);
			break;
		case ERROR_CODE_TABLE_NOT_EXIST:
			strncpy(buf, "table not exist", buflen);
			break;
		case ERROR_CODE_AUTH:
			strncpy(buf, "authentication faild", buflen);
			break;
		case ERROR_CODE_AGAIN:
			strncpy(buf, "try again", buflen);
			break;
		default:
			sprintf(buf, "unknown error:%d", errcode);
			break;
	}

	return buf;
}

int
http_echo(FCGX_Request *request, int code, const char *content, int length)
{
    int i;
	int ret;
	char retstr[4096];
    HttpString hs[256] = {
        {200,  "ok"},
        {400,  "Bad Request"},
		{409,  "Put Data Error"},
		{410,  "Create Table Error"},
		{411,  "Authentication Faild"},
        {418,  "Url Param Error"},
        {419,  "index have been add"},
        {420,  "hbase connect error"},
        {421,  "memlink connect error"},
        {422,  "index add error"},
        {423,  "index not exit"},
        {424,  "index remove error"},
        {425,  "index query error"},
        {425,  "index rebuild error"},
        {426,  "remove table error"},
        {427,  "unknown command"}
    };
    for (i = 0; i < 256; i++) {
        if (hs[i].code == code) {
			snprintf(retstr, 4096, "Status: %d %s\r\n"
								   "Content-Type: text/html\r\n"
								   "Content-Length: %d\r\n"
								   "\r\n", code, hs[i].str, length);

			ret = FCGX_PutS(retstr, request->out);
			if (ret < 0) {
				DERROR("fcgi pipe broken: %d->%s\n", code, content);
				return -1;
			}
            
            ret = FCGX_PutS(content, request->out);
			if (ret < 0) {
				DERROR("fcgi pipe broken: %d->%s\n", code, content);
				return -1;
			}
            return 0;
        }
    }
    return -1;
}

int
http_echo_error(FCGX_Request *request, int code, int errcode)
{
	int ret;
	char retstr[4096];

    HttpString hs[2] = {
        {200,  "ok"},
        {400,  "Request Failed"}
    };
    
    int idx = 0;
    if (code != 200)
    {
        idx = 1;
    }
    
    char content[128];
    snprintf(content, sizeof(content), "{\"result\":%d}", errcode);

    snprintf(retstr, sizeof(retstr), "Status: %d %s\r\n"
             "Content-Type: text/html\r\n"
             "Content-Length: %d\r\n"
             "\r\n", code, hs[idx].str, (int)strlen(content));
    
    ret = FCGX_PutS(retstr, request->out);
    if (ret < 0) {
        DERROR("fcgi pipe broken: %d->%s\n", code, content);
        return -1;
    }
    
    ret = FCGX_PutS(content, request->out);
    if (ret < 0) {
        DERROR("fcgi pipe broken: %d->%s\n", code, content);
        return -1;
    }
    return 0;
}

int
http_echo_attachment(FCGX_Request *request, int code, const char *content, int length)
{
    int i;
	int ret;
	char retstr[4096];
    HttpString hs[256] = {
        {200,  "ok"},
        {400,  "Bad Request"},
		{409,  "Put Data Error"},
		{410,  "Create Table Error"},
		{411,  "Authentication Faild"},
        {418,  "Url Param Error"},
        {419,  "index have been add"},
        {420,  "hbase connect error"},
        {421,  "memlink connect error"},
        {422,  "index add error"},
        {423,  "index not exit"},
        {424,  "index remove error"},
        {425,  "index query error"},
        {425,  "index rebuild error"},
        {426,  "remove table error"},
        {427,  "unknown command"}
    };
    for (i = 0; i < 256; i++) {
        if (hs[i].code == code) {
			snprintf(retstr, 4096, "Status: %d %s\r\n"
                     "Content-Disposition:attachment\r\n"
                     "Content-Type:application/octet-stream\r\n"
                     "Content-Length: %d\r\n"
                     "\r\n", code, hs[i].str, length);
            
			ret = FCGX_PutStr(retstr, strlen(retstr), request->out);
			if (ret < 0) {
				DERROR("fcgi pipe broken: %d->%s\n", code, content);
				return -1;
			}
            
            ret = FCGX_PutStr(content, length, request->out);
			if (ret < 0) {
				DERROR("fcgi pipe broken: %d->%s\n", code, content);
				return -1;
			}
            return 0;
        }
    }
    return -1;
}

